<?php
/*
 * @FilePath: SupplyController.php
 * @Author: 谭贤伟
 * @Date: 2023-04-01 14:06:25
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-04-07 12:44:45
 * @Descripttion: 
 */
namespace app\common\controller;

use app\admin\traits\Curd;

class SupplyController extends AdminController
{
    use Curd;
    
    public function initialize()
    {
        parent::initialize();
        $this->assign('data',[]);
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        // 1.验证请求方式
        if ($this->request->isPost()) {
            // 2.获取请求数据
            $post = $this->request->post();
            $validate = new $this->validate();
            if ($validate->check($post) == false) 
                return $this->error($validate->getError());
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            return $this->success('保存成功');
        }
        return $this->fetch('form');
    }

    /**
     * @NodeAnotation(title="编辑")
    */
    public function edit($id)
    {
        // 1.验证请求方式
        if ($this->request->isPost()) {
            // 2.获取请求数据
            $post = $this->request->post();

            $validate = new $this->validate();
            if ($validate->check($post) == false)
                return $this->error($validate->getError());

            $id = $this->request->param('id');

            $row = $this->model->find($id);
            empty($row) && $this->error('数据不存在');
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $list = $this->model->where('id', $id)->find();
        return $this->fetch('form', ['data' => $list]);
    }
}
